//
//  EvaluateViewController.m
//  JackDeve
//
//  Created by 王奎卿 on 2017/5/9.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "EvaluateViewController.h"
#import "OrderFromTableViewCell.h"
#import "OrderFromModel.h"
#import "OrderModel.h"
#import "AllOrderFromViewController.h"
#import "TTAccountTool.h"
@interface EvaluateViewController ()<UITableViewDelegate,UITableViewDataSource,OrderFromDelegate>
{
    float sumPrice;
    int sunNumber;
    NSInteger indexRow;
    NSInteger dinner;
}
@property (nonatomic,strong) NSMutableArray <OrderModel *> *dataArr;
@property (nonatomic,strong) NSString *str;
@end

@implementation EvaluateViewController

//- (void)viewWillDisappear:(BOOL)animated
//{
//    [self removeFromParentViewController];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.allOrderV = self.view;
    
    [self showAllOrderView];
    
    // 获取数据
    [self getOrderData];
    
    // 注册单元格
    [self regisCell];
}

- (void)getOrderData
{
    OrderFromModel *dataModel = [[OrderFromModel alloc]init];
    Data *data = [HomeUserDefault getUserDefault];
    _str = [NSString stringWithFormat:@"http://daohe.apic.1by1.cc/orders/index?token=%@&page_id=1&commented=1",data.token];
    [dataModel getOrderData:_str];

    dataModel.delegate = self;
}

- (void)postOrderJson:(NSMutableArray *)array
{
    _dataArr = array;
    [self.tableView reloadData];
}

- (void)regisCell
{
    [self.tableView registerClass:[OrderFromTableViewCell class] forCellReuseIdentifier:@"evalua"];
}


- (void)showAllOrderView
{
    // 对tableView进行包装
    CGSize size = [UIScreen mainScreen].bounds.size;
    self.tableView = ({
        
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 110, size.width, size.height-110) style:UITableViewStylePlain];
        
        tableView.delegate = self;
        
        tableView.dataSource = self;
        
        tableView.tableFooterView = [[UIView alloc] init];
        
        tableView;
    });
    
    [self.view addSubview:self.tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    indexRow = indexPath.section;
    
    OrderFromTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"evalua"];
    
//    if (!cell)
//    {
//        cell = [[OrderFromTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"22222"];
//    }
    
    //订单状态：20已支付或到付，25商家处理，30完成
    
    cell.order_status.text = @"订单完成";
    //判断商品个数，进行赋值
    if (_dataArr[indexPath.section].goods.count == 0) {
        cell.goods_one.hidden = YES;
        cell.count_one.hidden = YES;
        cell.goods_two.hidden = YES;
        cell.count_two.hidden = YES;
        cell.goods_three.hidden = YES;
        cell.count_three.hidden = YES;
        cell.total.hidden = YES;
        cell.total_price.hidden = YES;
        cell.ellipsis.hidden = YES;
        
    }if (_dataArr[indexPath.section].goods.count == 1) {
        cell.goods_one.text = [_dataArr[indexPath.section].goods[0] objectForKey:@"goods_name"];
        cell.count_one.text = [NSString stringWithFormat:@"X%@", [_dataArr[indexPath.section].goods[0] objectForKey:@"num"]];
        
        cell.goods_two.hidden = YES;
        cell.count_two.hidden = YES;
        cell.goods_three.hidden = YES;
        cell.count_three.hidden = YES;
        cell.ellipsis.hidden = YES;
    }if (_dataArr[indexPath.section].goods.count == 2) {
        cell.goods_one.text = [_dataArr[indexPath.section].goods[0] objectForKey:@"goods_name"];
        cell.count_one.text = [NSString stringWithFormat:@"X%@", [_dataArr[indexPath.section].goods[0] objectForKey:@"num"]];
        cell.goods_two.text = [_dataArr[indexPath.section].goods[1] objectForKey:@"goods_name"];
        cell.count_two.text = [NSString stringWithFormat:@"X%@",[_dataArr[indexPath.section].goods[1] objectForKey:@"num"]];
        
        cell.goods_three.hidden = YES;
        cell.count_three.hidden = YES;
        cell.ellipsis.hidden = YES;
    }if (_dataArr[indexPath.section].goods.count == 3) {
        cell.goods_one.text = [_dataArr[indexPath.section].goods[0] objectForKey:@"goods_name"];
        cell.count_one.text = [NSString stringWithFormat:@"X%@", [_dataArr[indexPath.section].goods[0] objectForKey:@"num"]];
        cell.goods_two.text = [_dataArr[indexPath.section].goods[1] objectForKey:@"goods_name"];
        cell.count_two.text = [NSString stringWithFormat:@"X%@", [_dataArr[indexPath.section].goods[1] objectForKey:@"num"]];
        cell.goods_three.text = [_dataArr[indexPath.section].goods[2] objectForKey:@"goods_name"];
        cell.count_three.text = [NSString stringWithFormat:@"X%@",[_dataArr[indexPath.section].goods[2] objectForKey:@"num"]];
        
        cell.ellipsis.hidden = YES;
    }if (_dataArr[indexPath.section].goods.count >3) {
        cell.goods_one.text = [_dataArr[indexPath.section].goods[0] objectForKey:@"goods_name"];
        cell.count_one.text = [NSString stringWithFormat:@"X%@", [_dataArr[indexPath.section].goods[0] objectForKey:@"num"]];
        cell.goods_two.text = [_dataArr[indexPath.section].goods[1] objectForKey:@"goods_name"];
        cell.count_two.text = [NSString stringWithFormat:@"X%@", [_dataArr[indexPath.section].goods[1] objectForKey:@"num"]];
        cell.goods_three.text = [_dataArr[indexPath.section].goods[2] objectForKey:@"goods_name"];
        cell.count_three.text = [NSString stringWithFormat:@"X%@",[_dataArr[indexPath.section].goods[2] objectForKey:@"num"]];
    }
    
    for (int i = 0; i < _dataArr[indexPath.section].goods.count; i++) {
        int num = [[_dataArr[indexPath.section].goods[i] objectForKey:@"num"] intValue];
        
        float price = [[_dataArr[indexPath.section].goods[i] objectForKey:@"goods_price"] floatValue];
        //、、个数
        sunNumber += num;
        //总价
        sumPrice += num*price;
    }
    
    Data *data = [HomeUserDefault getUserDefault];
    [cell.userImg sd_setImageWithURL:[NSURL URLWithString:data.avatar]];

    
    cell.diner_name.text = _dataArr[indexPath.section].diner_name;
    cell.total.text = [NSString stringWithFormat:@"共%d件商品,实付",sunNumber];
    cell.total_price.text = [NSString stringWithFormat:@"¥%.2f",sumPrice];
    
    sumPrice = 0.0;
    sunNumber = 0;
    
    // 取消选中状态
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    [cell.cancelBtn setTitle:@"评价" forState:UIControlStateNormal];
    [cell.cancelBtn addTarget:self action:@selector(pingjia) forControlEvents:UIControlEventTouchUpInside];
    [cell.againBtn setTitle:@"再来一单" forState:UIControlStateNormal];
    [cell.againBtn addTarget:self action:@selector(again) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}
// 按钮点击方法
- (void)pingjia
{
    NSLog(@"评价");
}
#pragma mark 再来一单
-(void)again{
    NSLog(@"这杯干完还有一杯");
    OrderModel *sub = _dataArr[indexRow];
    Data *login = [HomeUserDefault getUserDefault];
    NSString *tr = [NSString stringWithFormat:@"http://daohe.apic.1by1.cc/orders/buy_again?token=%@&order_sn=%@",login.token,sub.order_sn];
    TTAccountTool *account = [[TTAccountTool alloc] init];
    [account againDingDan:tr];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    editingStyle = UITableViewCellEditingStyleDelete;
    
    if (indexRow >= _dataArr.count) {
        return;
    }
    
    OrderModel *model = _dataArr[indexRow];
    Data *data = [HomeUserDefault getUserDefault];
    
    NSString *tr = [NSString stringWithFormat:@"http://daohe.apic.1by1.cc/orders/delete?token=%@&order_sn=%@",data.token,model.order_sn];
    TTAccountTool *account = [[TTAccountTool alloc] init];
    [account deleteDingDan:tr];
    
    [self.tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height;
    
    if (self.dataArr[indexRow].goods.count == 0) {
        height = 0;
        return height;
    }if (self.dataArr[indexRow].goods.count == 1) {
        height = 160;
        return height;
    }if (self.dataArr[indexRow].goods.count == 2) {
        height = 190;
        return height;
    }if (self.dataArr[indexRow].goods.count == 3) {
        height = 220;
        return height;
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
